﻿using System;
using System.Data;
using System.Collections.Generic;

using Catering_Model;
namespace Catering_Rules
{
    /// <summary>
    /// ClassInfo
    /// </summary>
    public partial class ClassInfo
    {
        private readonly Catering_Operation.ClassInfo dal = new Catering_Operation.ClassInfo();
        public ClassInfo()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.ClassInfo model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.ClassInfo model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.ClassInfo GetModel(int id)
        {

            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Catering_Model.ClassInfo GetModelByCache(int id)
        {

            string CacheKey = "ClassInfoModel-" + id;
            object objModel = Catering_DBOpertion.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Catering_DBOpertion.ConfigHelper.GetConfigInt("ModelCache");
                        Catering_DBOpertion.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Catering_Model.ClassInfo)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        public Catering_Model.ClassInfo GetModelByModelName(string ModelName)
        {
            Catering_Model.ClassInfo model = new Catering_Model.ClassInfo();
            string where = "CName='" + ModelName + "' ";
            DataTable dt = GetList(where).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["id"] != null && dt.Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(dt.Rows[0]["id"].ToString());
                }
                if (dt.Rows[0]["CName"] != null && dt.Rows[0]["CName"].ToString() != "")
                {
                    model.CName = dt.Rows[0]["CName"].ToString();
                }
                if (dt.Rows[0]["AreaID"] != null && dt.Rows[0]["AreaID"].ToString() != "")
                {
                    model.AreaID = int.Parse(dt.Rows[0]["AreaID"].ToString());
                }
                if (dt.Rows[0]["Back"] != null && dt.Rows[0]["Back"].ToString() != "")
                {
                    model.Back = dt.Rows[0]["Back"].ToString();
                }
                if (dt.Rows[0]["temp1"] != null && dt.Rows[0]["temp1"].ToString() != "")
                {
                    model.temp1 = dt.Rows[0]["temp1"].ToString();
                }
                if (dt.Rows[0]["AID"] != null && dt.Rows[0]["AID"].ToString() != "")
                {
                    model.AID = int.Parse(dt.Rows[0]["AID"].ToString());
                }
                dt.Dispose();
            }
            return model;
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.ClassInfo> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }

        public DataTable GetModelList(string OAREAID, string CLASSID, string CompanyID)
        {
            string whereClause = "";
            if (!string.IsNullOrEmpty(CompanyID))
            {
                whereClause += " AND CompanyID=" + CompanyID;
            }
            if (!string.IsNullOrEmpty(OAREAID) && OAREAID != "-10")
            {
                whereClause += " AND AreaID=" + OAREAID;
            }
            if (!string.IsNullOrEmpty(CLASSID) && CLASSID != "-10")
            {
                whereClause += " AND ClassID=" + CLASSID;
            }

            DataTable tempdt = new Company().GetView_CompanyList(whereClause).Tables[0];
            if (tempdt != null && tempdt.Rows.Count > 0)
            {
                return tempdt;
            }
            return null;
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.ClassInfo> DataTableToList(DataTable dt)
        {
            List<Catering_Model.ClassInfo> modelList = new List<Catering_Model.ClassInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Catering_Model.ClassInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Catering_Model.ClassInfo();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["CName"] != null && dt.Rows[n]["CName"].ToString() != "")
                    {
                        model.CName = dt.Rows[n]["CName"].ToString();
                    }
                    if (dt.Rows[n]["AreaID"] != null && dt.Rows[n]["AreaID"].ToString() != "")
                    {
                        model.AreaID = int.Parse(dt.Rows[n]["AreaID"].ToString());
                    }
                    if (dt.Rows[n]["Back"] != null && dt.Rows[n]["Back"].ToString() != "")
                    {
                        model.Back = dt.Rows[n]["Back"].ToString();
                    }
                    if (dt.Rows[n]["temp1"] != null && dt.Rows[n]["temp1"].ToString() != "")
                    {
                        model.temp1 = dt.Rows[n]["temp1"].ToString();
                    }
                    if (dt.Rows[n]["AID"] != null && dt.Rows[n]["AID"].ToString() != "")
                    {
                        model.AID = int.Parse(dt.Rows[n]["AID"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.ClassInfo> GetAllList(int abc)
        {
            List<Catering_Model.ClassInfo> listClass = new List<Catering_Model.ClassInfo>();
            DataTable dt = dal.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Catering_Model.ClassInfo temp = new Catering_Model.ClassInfo();
                int id = 0;
                Int32.TryParse(dt.Rows[i]["id"].ToString(), out id);
                temp.id = id;
                temp.CName = dt.Rows[i]["CName"].ToString();
                int AreaId = 0;
                Int32.TryParse(dt.Rows[i]["AreaID"].ToString(), out AreaId);
                temp.AreaID = AreaId;
                listClass.Add(temp);
            }
            return listClass;
        }

        /// <summary>
        /// 获取用户所在公司下的所有班组
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        public DataTable GetClassIDListByPersonnelID(int PID)
        {
            Catering_Model.E_Personnel PSM = new Catering_Operation.D_Personnel().GetModel(PID);
            if (PSM != null && PSM.ClassID > 0)
            {
                return dal.GetCommpanyClassIDListByClassID(PSM.ClassID);
            }
            return null;
        }

        /// <summary>
        /// 获取用户所在公司下的所有班组
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        public string GetStrClassIDListByPersonnelID(string StrPID)
        {
            string result = "";
            int PID = 0;
            Int32.TryParse(StrPID, out PID);
            if (PID > 0)
            {
                DataTable dt = GetClassIDListByPersonnelID(PID);
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int classid = Convert.ToInt32(dt.Rows[i]["ClassID"]);
                        if (classid > 0) result += classid + ",";
                    }
                }
            }
            return result.ToString().Length > 0 ? result.ToString().TrimEnd(',') : "";
        }

        public DataTable GetClassIDListByGroupID(int GroupID)
        {
            return dal.GetClassIDListByGroupID(GroupID);
        }

        public string GetStrtClassIDListByGroupID(string StrGroupID)
        {
            string result = "";
            int GroupID = 0;
            Int32.TryParse(StrGroupID, out GroupID);
            if (GroupID > 0)
            {
                DataTable dt = GetClassIDListByGroupID(GroupID);
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int classid = Convert.ToInt32(dt.Rows[i]["ID"]);
                        if (classid > 0) result += classid + ",";
                    }
                }
            }
            return result.ToString().Length > 0 ? result.ToString().TrimEnd(',') : "";
        }

        /// <summary>
        /// 同步作业区
        /// </summary>
        /// <param name="NewAreaID"></param>
        /// <param name="ClassID"></param>
        /// <returns></returns>
        public bool UpdateArearIDByClassID(int NewAreaID, int ClassID)
        {
            return dal.UpdateArearIDByClassID(NewAreaID, ClassID);
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

